<?php
require_once(dirname(dirname(dirname(__FILE__))) . '/core.php');
?><!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>订单列表</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" type="text/css" href="../../css/reset.css">
    <link rel="stylesheet" type="text/css" href="../../css/main.css">
    <link rel="stylesheet" type="text/css" href="../../css/member.css">
    <link rel="stylesheet" type="text/css" href="/wap/css/font-awesome.min.css">
    <style>
        .evalution_order {
            background: #DA4F49;
            padding: 5px 20px;
            color: #fff;
        }

        #bottom .nav {
            background: #444;
        }

        header, .header, .header-wrap {
            background: #f8f8f8;
        }

        ul {
            list-style: none !important;
            padding: 0;
        }
    </style>
</head>
<body>
<header id="header"></header>

<div class="order-list-wp" id="order-list"></div>
<script type="text/html" id="order-list-tmpl">
    <div class="order-list">
        <%if (order_group_list.length >0){%>
        <ul>
            <%for(var i = 0;i
            <order_group_list.length
              ;i++){
              var orderlist = order_group_list[i].order_list;
              %>
                <li class="<%if(order_group_list[i].pay_amount){%>green-order-skin<%}else{%>gray-order-skin<%}%> <%if(i>0){%>mt10<%}%>">
                    <div class="order-ltlt">
                        <p>
                            下单时间：
                            <%=$getLocalTime(order_group_list[i].add_time)%>
                        </p>
                    </div>
                    <% for(var j = 0;j
                    <orderlist.length
                      ;j++){
                      var order_goods = orderlist[j].extend_order_goods;
                      %>
                        <div class="order-lcnt">
                            <div class="order-lcnt-shop">
                                <p>店铺名称：<%=orderlist[j].store_name%></p>
                                <p>订单编号：<%=orderlist[j].order_sn%>
                                    <%if (orderlist[j].is_we_groupbuy > 1){ %>
                                    <span style="color: red;">（微团订单）</span>
                                    <% } %>
                                </p>
                                <p>收货信息：<%=orderlist[j].receiver_name%>,&nbsp;<%=orderlist[j].phone%>,&nbsp;<%=orderlist[j].address%>

                                </p>
                            </div>
                            <div class="order-shop-pd">
                                <%for (var k = 0;k
                                <order_goods.length
                                  ;k++){%>
                                    <a class="order-ldetail clearfix <%if(k>0){%>bd-t-de<%}%>"
                                       href="<%=WapSiteUrl%>/tmpl/product_detail.html?goods_id=<%=order_goods[k].goods_id%>">
                                            <span class="order-pdpic">
                                                <img src="<%=order_goods[k].goods_image_url%>"/>
                                            </span>
                                        <div class="order-pdinfor">
                                            <p><%=order_goods[k].goods_name%></p>
                                            <p>
                                                单价：<span class="clr-d94">￥<%=order_goods[k].goods_price%></span>
                                            </p>
                                            <%if (order_goods[k].tax) { %>
                                            <p>
                                                税金：<span class="clr-d94">￥<%=order_goods[k].tax%></span>
                                            </p>
                                            <% } %>
                                            <p>
                                                商品数量：<%=order_goods[k].goods_num%>
                                            </p>
                                        </div>
                                    </a>
                                    <%}%>
                            </div>
                            <div class="order-shop-total">
                                <p>运费：<span class="clr-d94">￥<%=orderlist[j].shipping_fee%></span></p>
                                <% if(orderlist[j].pd_amount>0 || orderlist[j].rcb_amount>0) { %>
                                <p>预支付：<span class="clr-green">￥<%=orderlist[j].pd_amount%>(预存款支付) + <%=orderlist[j].rcb_amount%>(充值卡支付)</span></p>
                                <% } %>
                                <% if (orderlist[j].proAmount>0){ %>
                                <p>优惠价格：<span class="clr-green">￥<%=orderlist[j].proAmount%></span></p>
                                <% } %>
                                <p class="clr-c07">合计：￥<%=orderlist[j].order_amount%> </p>
                                <p class="mt5">
                                    <%
                                    var stateClass ="ot-finish";
                                    var orderstate = orderlist[j].order_state;
                                    if(orderstate == 20 || orderstate == 30 || orderstate == 40){
                                        stateClass = stateClass;
                                    }else if(orderstate == 0) {
                                        stateClass = "ot-cancel";
                                    }else {
                                        stateClass = "ot-nofinish";
                                    }
                                    %>
                                    <span class="<%=stateClass%>"><%=orderlist[j].state_desc%></span>
                                </p>
                                <p class="mt5">
                                    <%if(orderlist[j].if_receive){%>
                                    <a href="javascript:void(0)" order_id="<%=orderlist[j].order_id%>"
                                       class="sure-order">确认订单</a>
                                    <%}%>
                                    <%if(orderlist[j].if_refund_cancel){%>
                                    <a href="javascript:void(0)" order_id="<%=orderlist[j].order_id%>"
                                       class="refund-order">申请退款</a>
                                    <%}%>
                                    <%if(orderlist[j].if_cancel){%>
                                    <a href="javascript:void(0)" order_id="<%=orderlist[j].order_id%>"
                                       class="cancel-order">取消订单</a>
                                    <%}%>
                                    <%if(orderlist[j].if_deliver){%>
                                    <a href="javascript:void(0)" order_id="<%=orderlist[j].order_id%>"
                                       class="viewdelivery-order">查看物流</a>
                                    <%}%>
                                    <%if(orderlist[j].evaluation_state == 0 && orderlist[j].order_state == 40){%>
                                    <a href="/wap/tmpl/member/evalution.php?order_id=<%=orderlist[j].order_id%>"
                                       style=order_id="<%=orderlist[j].order_id%>" class="evalution_order">我要评价</a>
                                    <%}%>


                                </p>
                            </div>
                        </div>
                        <%}%>
                        <%if(order_group_list[i].pay_amount && order_group_list[i].pay_amount>0){%>
                        <a href="<%=ApiUrl %>/index.php?act=member_payment&op=pay&key=<%=key %>&pay_sn=<%=order_group_list[i].pay_sn %>"
                           class="l-btn-login check-payment">订单支付（￥<%= p2f(order_group_list[i].pay_amount) %>）</a>
                        <%}%>
                </li>
                <%}%>
        </ul>
        <div class="pagination mt10">
            <a href="javascript:void(0);" class="pre-page <%if(curpage <=1 ){%>disabled<%}%>">上一页</a>
            <a href="javascript:void(0);" has_more="<%if (hasmore){%>true<%}else{%>false<%}%>"
               class="next-page <%if (!hasmore){%>disabled<%}%>">下一页</a>
        </div>
        <%}else {%>
        <div class="no-record">
            暂无记录
        </div>
        <%}%>
    </div>
</script>
<div id="footer"></div>

<style>
    #paylist {
        position: fixed;
        width: 100%;
        bottom: 0;
        left: 0;
        z-index: 10000;
        display: none;
        background: #fff;
        border-top: 1px solid #ddd;
    }

    #paylist a {
        display: block;
        text-align: center;
        margin-bottom: 5px;
    }

    #paylist a:first-child {
    }

    #paylist #tle {
        font-size: .8em;
        padding-left: .5em;
        line-height: 4em;
    }

    #cnt {
        width: 240px;
        margin: 0 auto;
    }

    #cnt a {
        float: left;
        display: block;
        color: #555;
        margin: 10px;
        font-size: .8em;
    }
</style>
<div id="paylist">
    <div id='tle'>请选择您的支付方式</div>
    <div id='cnt'>
        <?php
        $model_mb_payment = Model('mb_payment');
        $mb_payments = $model_mb_payment->getMbPaymentOpenList(); // 已开启支付方式代
        $mb_payment_codes = array(); // 已开启支付方式代码标识
        foreach($mb_payments as $val){
            array_push($mb_payment_codes, $val["payment_code"]);
        }

        $model_payment = Model('payment');
        $payments = $model_payment->getPaymentOpenList(); // 已开启支付方式代
        $payment_codes = array(); // 已开启支付方式代码标识
        foreach($payments as $val){
            array_push($payment_codes, $val["payment_code"]);
        }

        // 用户浏览器
        $user_agent = $_SERVER['HTTP_USER_AGENT'];

        // 如果在PC，不显示微信支付
        if (strpos($user_agent, 'MicroMessenger') !== false && in_array("wxpay", $mb_payment_codes) ) {
            ?>
            <a id='wechat' href="javascript:;">
                <img src="/wap/payicon/icon_wechat.png" width='50px' alt="">
                <p>
                    微信支付
                </p>
            </a>
            <?php
        }
        //如果在微信里，不显示支付宝
        if (strpos($user_agent, 'MicroMessenger') === false && in_array("alipay", $mb_payment_codes) ) {

            ?>
            <a id='alipay' href="javascript:;">
                <img src="/wap/payicon/icon_alipay.png" width='50px' alt="">
                <p>
                    支付宝
                </p>
            </a>
            <?php
        }
        ?>
        <?php
        if( in_array("abcpay", $mb_payment_codes)){
            ?>
            <a id='abcpay' href="javascript:;">
                <img src="/wap/payicon/icon_abc.png" width='50px' alt="">
                <p>
                    农行商E付
                </p>
            </a>
        <?php } ?>

        <?php
        if( in_array("abcpay2", $mb_payment_codes)){
            ?>
            <a id='abcpay2' href="javascript:;">
                <img src="/wap/payicon/icon_abc2.png" width='50px' alt="">
                <p>
                    农行商E付2
                </p>
            </a>
        <?php } ?>

        <?php
        if(strpos($user_agent, 'MicroMessenger') === false && in_array("joinpay1", $mb_payment_codes)){
            ?>
            <a id='joinpay1' href="javascript:;">
                <img src="/wap/payicon/icon_joinpay1.png" width='50px' height="50px" alt="">
                <p>
                    汇聚H5支付
                </p>
            </a>
        <?php } ?>

        <?php
        if(strpos($user_agent, 'MicroMessenger') !== false && in_array("joinpay2", $mb_payment_codes)){
            ?>
            <a id='joinpay2' href="javascript:;">
                <img src="/wap/payicon/icon_joinpay2.png" width='50px' height="50px" alt="">
                <p>
                    汇聚公众号支付
                </p>
            </a>
        <?php } ?>

        <?php
        if( in_array("tl2", $payment_codes) ){
            ?>
            <a id='tl2' href="javascript:;">
                <img src="/shop/templates/default/images/payment/tl2_logo.gif" width='50px' alt="">
                <p>
                    通联支付
                </p>
            </a>
            <a id='tl4' href="javascript:;">
                <img src="/shop/templates/default/images/payment/tl2_logo.gif" width='50px' alt="">
                <p>
                    通联微支付
                </p>
            </a>
            <?php
        }
        if( in_array("tl3", $payment_codes) && (strpos($user_agent, 'MicroMessenger') === false) ){
            ?>
            <a id='tl3' href="javascript:;">
                <img src="/shop/templates/default/images/payment/tl2_logo.gif" width='50px' alt="">
                <p>
                    预付卡支付
                </p>
            </a>

            <?php
        }
        ?>
    </div>
</div>

<div id='qr_code_box' style='display:none'>

</div>

<div id='joinpay1box' style="display:none">
    <a href="javascript:;" p="ALIPAY_H5">支付宝</a>
</div>

<script type="text/javascript" src="../../js/zepto.min.js"></script>
<script type="text/javascript" src="../../js/template.js"></script>
<script type="text/javascript" src="../../js/config.js"></script>
<script type="text/javascript" src="../../js/common.js"></script>
<script type="text/javascript" src="../../js/simple-plugin.js"></script>
<script type="text/javascript" src="../../js/tmpl/common-top.js"></script>
<script type="text/javascript" src="../../js/tmpl/footer.js"></script>
<script type="text/javascript" src="../../js/tmpl/order_list.js"></script>
<script src="/wxpay/qrcode.js"></script>

<script src='/wap/js/layer.m/layer.m.js'></script>

<?php
require_once(dirname(dirname(dirname(__FILE__))) . '/wechat_core.php');

if (strpos($user_agent, 'MicroMessenger') !== false) {
//            require_wechat_js();

    ?>
    <script>
        $('#alipay').hide();
    </script>
    <?php
}
?>

<?php
if(isset($_POST['retCode'])){
    if($_POST['retCode'] == '0000'){
        file_get_contents(SHOP_SITE_URL . '/api/payment/tl3/notify_url.php?' . http_build_query($_POST));
        ?>
        <script>
            layermsg("支付成功",function () {
                location.reload()
            });
        </script>
    <?php
    }
    else{
    ?>
        <script>
            layermsg("支付失败",function () {
                location.reload()
            });
        </script>
        <?php
    }
}
?>
<script>
    $(document).on('click', '.check-payment', function () {
        var alipay = this.href;
        var pay_sn = alipay.match(/pay_sn=([^&]+)/)[1];
        $('#paylist').attr('payurl', alipay).show();
        return false;
    });


    $('#paylist a').click(function () {
        if (this.id == 'alipay') {
            location.href = $(this).parents('#paylist').attr('payurl')+'&payment_type=alipay';
        }else if(this.id == 'abcpay'){
            location.href = $(this).parents('#paylist').attr('payurl')+'&payment_type=abcpay';
        }else if(this.id == 'abcpay2'){
            location.href = $(this).parents('#paylist').attr('payurl')+'&payment_type=abcpay2';
        }
        else if(this.id == 'joinpay1'){
            var href = $(this).parents('#paylist').attr('payurl');
            var paysn = href.match(/pay_sn=([^\&]+)/)[1];
            layer.open({
                area: ['420px', '240px'], //宽高
                content: $("#joinpay1box").html(),
                title: "选择需要使用的支付方式"
            });
            $('[p]').click(function () {
                var p = $(this).attr('p');
                $.ajax({
                    url: '/mobile/index.php?act=member_payment&op=joinpay1',
                    type: "post",
                    data: {
                        pay_sn : paysn,
                        key : getcookie('key'),
                        pay_type: p
                    },
                    dataType:"json",
                    success: function(res){
                        var data = res.datas;
                        if(data.error || res.code!=200){
                            return layermsg("支付失败["+data.error+"]");
                        }else{
                            layer.closeAll();
                            document.write(data["rc_Result"]);
                        }
                    }
                });

            });

        }else if(this.id == 'joinpay2'){
            var href = $(this).parents('#paylist').attr('payurl');
            var paysn = href.match(/pay_sn=([^\&]+)/)[1];

            $.ajax({
                url: '/mobile/?act=member_payment&op=joinpay2',
                type: "post",
                data: {
                    pay_sn : paysn,
                    openid: getcookie('openid'),
                    key : getcookie('key')
                },
                dataType:"json",
                success: function(res){
                    var data = res.datas;
                    if(data.error || res.code!=200){
                        return layermsg("支付失败["+data.error+"]");
                    }else{
                        var json = eval('(' + data["rc_Result"] + ')');
                        json_data = json;
                        console.log(json_data);
                        callpay();
                    }
                }
            });
        }
        else if(this.id == 'tl2'){
            var href = $(this).parents('#paylist').attr('payurl');
            var paysn = href.match(/pay_sn=([^\&]+)/)[1];
            $.post('/mobile/?act=member_payment&op=tonglianpay',{
                pay_sn : paysn,
                key : getcookie('key')
            },function(msg){
                if(msg == 'failed'){
                    return layermsg("支付失败");
                }
                creat_qr_img(msg)
            });
        }
        else if(this.id == 'tl3'){
            var index = layer.open({type: 2});
            var href = $(this).parents('#paylist').attr('payurl');
            var paysn = href.match(/pay_sn=([^\&]+)/)[1];
            $.get('/shop/?act=payment&op=get_tl3_url&pay_sn=' + paysn,function (url) {
                location.href = url;
            });
        }
        else if(this.id == 'tl4'){
            var index = layer.open({type: 2});
            var href = $(this).parents('#paylist').attr('payurl');
            var paysn = href.match(/pay_sn=([^\&]+)/)[1];
            $.get('/shop/?act=payment&op=get_tl4_url&pay_sn=' + paysn,function (msg) {
                layer.closeAll();
                document.write(msg);
            });
        }
        else {
            var index = layer.open({type: 2});
            var link = $('#paylist').attr('payurl');
            var pay_sn = link.match(/pay_sn=([^&]+)$/)[1];
            // $.get()
            $.get('/shop/index.php?act=wxpay&op=mobile_pay', {
                pay_sn: pay_sn,
                openid: getcookie('openid')
            }, function (msg) {
                var json = eval('(' + msg + ')');
                json_data = json;
                console.log(json_data);
                callpay();
            });
        }
    });

    function creat_qr_img(url){
        //参数1表示图像大小，取值范围1-10；参数2表示质量，取值范围'L','M','Q','H'
        var qr = qrcode(10, 'M');
        qr.addData(url);
        qr.make();
        var wording=document.createElement('p');
        wording.innerHTML = "长按二维码进行识别";
        var code=document.createElement('DIV');
        code.innerHTML = qr.createImgTag();
        var element=$('#qr_code_box')[0];
        element.appendChild(wording);
        element.appendChild(code);
        layer.open({
            type : 2,
            content : '请稍后',
        });
        setTimeout(function(){
            $('.layui-m-layercont').html('').append($('#qr_code_box').clone(true).clone(true).show())
        },500)
    }

    var json_data = '';
    function jsApiCall() {
        WeixinJSBridge.invoke(
          'getBrandWCPayRequest',
          json_data,
          function (res) {
              //console.log(res)
              //document.write(JSON.stringify(res));
              layer.closeAll();
              if (res.err_msg == "get_brand_wcpay_request:ok") {
                  location.href = '/wap/tmpl/member/order_list.php';
                  // $.get('/shop/')
              }
              // WeixinJSBridge.log(res.err_msg);
              // alert(res.err_code+res.err_desc+res.err_msg);
          }
        );
    }

    function callpay() {
//            wx.chooseWXPay({
//                timestamp: 0, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
//                nonceStr: '', // 支付签名随机串，不长于 32 位
//                package: '', // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）
//                signType: '', // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
//                paySign: '', // 支付签名
//                success: function (res) {
//                    // 支付成功后的回调函数
//                }
//            })
        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
            }
        } else {
            jsApiCall();
        }
    }
</script>

</body>
</html>